package com.gtqing.mall.service.impl;

import com.gtqing.mall.consts.MallConst;
import com.gtqing.mall.dao.CategoryMapper;
import com.gtqing.mall.pojo.Category;
import com.gtqing.mall.service.ICategoryService;
import com.gtqing.mall.vo.CategoryVo;
import com.gtqing.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author gthang
 * @create 2022-04-11-22:36
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 耗時：http（請求微信api ） > 磁盤 > 內存
     * mysql:(磁盤 + 內存)
     * @return
     */

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
//        List<CategoryVo> categoryVoList = new ArrayList<>();
        List<Category> categories = categoryMapper.selectAll();

        //查出parent_id=0
//        for (Category category : categories){
//            if (category.getParentId().equals(MallConst.ROOT_PARENR_ID)){
//                CategoryVo categoryVo = new CategoryVo();
//                BeanUtils.copyProperties(category,categoryVo);
//                categoryVoList.add(categoryVo);
//            }
//        }

        //lambda + stream
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(MallConst.ROOT_PARENR_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());

        //查詢子目錄

        findSubCategory(categoryVoList,categories);

        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categories = categoryMapper.selectAll();
        findSubCategoryId(id,resultSet,categories);

    }

    private void findSubCategoryId(Integer id,Set<Integer> resultSet,List<Category> categories){
        for(Category category : categories){
            if(category.getParentId().equals(id)){
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(),resultSet,categories) ;
            }
        }

    }


    private void  findSubCategory(List<CategoryVo> categoryVoList,List<Category> categories){
        for(CategoryVo categoryVo : categoryVoList){

            List<CategoryVo> subCategoryVoList = new ArrayList<>();

            for (Category category : categories){
                //如果查到內容，設置subCategory,繼續往下查
                if (categoryVo.getId().equals(category.getParentId())){
                    CategoryVo subCategoryVo = category2CategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }

                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setSubCategories(subCategoryVoList);

                findSubCategory(subCategoryVoList,categories);
            }
        }
    }





    private  CategoryVo  category2CategoryVo(Category category){
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }
}
